import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/views/Layout.vue'
import Register from '@/views/Register.vue'
const routes = [
  {
    path: '/statistics',
    name: 'StatisticsView',
    component: () => import('@/views/StatisticsView.vue') // 动态加载组件
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue')
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('@/views/Home.vue')
      },
      {
        path: '/category/:filterType/:value',
        name: 'CategoryList',
        component: () => import('@/views/category/CategoryList.vue')
      },
      {
        path: '/search',
        name: 'SearchResult',
        component: () => import('@/views/search/SearchResult.vue')
      }
    ]
  },
  {
    path: '/product/:id',
    name: 'ProductDetail',
    component: () => import('@/views/product/ProductDetail.vue')
  },
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: () => import('@/views/admin/AdminLogin.vue')
  },
  {
    path: '/admin/manage',
    name: 'ProductManage',
    component: () => import('@/views/admin/ProductManage.vue'),
    beforeEnter: (to, from, next) => {
      if (localStorage.getItem('token')) {
        next()
      } else {
        next('/admin/login')
      }
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router 